package com.aigou.admin.controller;

import com.aigou.admin.entity.User;
import com.aigou.admin.service.UserService;
import com.aigou.admin.service.impl.UserServiceImpl;
import com.alibaba.fastjson2.JSON;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/findAllUsers")
/**
 * 查询所有用户信息的Servlet
 * 定义了 FindAllUsersController类，该类继承自 HttpServlet
 * HttpServlet 是 Java Servlet API 中的一个抽象类，用于处理 HTTP 请求
 */
public class FindAllUsersController extends HttpServlet {
    //调用service
    private UserService userService = new UserServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询用户
        List<User> users = userService.findAll();
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(users));

    }
}
